/**
 * @Author: 罗显
 * @CreateTime: 2025-05-07
 * @Version: 1.0
 */


package com.it.material.controller;

import com.it.material.entity.NewsEntity;
import com.it.material.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @PostMapping("/publish")
    public ResponseEntity<String> publishNews(
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("channelId") Integer channelId,
            @RequestParam("userId") Long userId,
            @RequestParam("status") NewsEntity.Status status,
            @RequestParam(value = "coverImages", required = false) MultipartFile[] coverImages // 允许为空
    ) {
        NewsEntity newsEntity = new NewsEntity();
        newsEntity.setTitle(title);
        newsEntity.setContent(content);
        newsEntity.setChannelId(channelId);
        newsEntity.setUserId(userId);
        newsEntity.setStatus(status);
        // 设置其他属性

        try {
            newsService.publishNews(newsEntity, coverImages);
            return ResponseEntity.ok("文章发布成功！");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文章发布失败：" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文章发布失败，未知错误：" + e.getMessage());
        }
    }
}